package com.wzgl.core.webmagic.pipeline;

import com.jfinal.aop.Duang;
import com.jfinal.plugin.redis.Redis;
import com.wzgl.core.CacheConsts;
import com.wzgl.core.model.Tgrid;
import com.wzgl.core.server.GirdService;
import com.wzgl.core.utils.WZGLUtils;

import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

public class TgridsPipeLine implements Pipeline  {
	GirdService service = Duang.duang(GirdService.class);
	String pic_ = "http://meiwen.59xihuan.cn";
	String pic_2 = "http://www.gamersky.com/pcgame";
	String pic_3 = "http://www.gamersky.com/handbook/game/gl";
	String pic_4 = "http://play.163.com/shouyou";
	String pic_5 = "http://gg.163.com/";
	String pic_6 = "http://www.yxdown.com/news/";
	String pic_7 = "http://news.17173.com/dalu/";
	String pic_8 = "http://top.sina.com.cn/news/show/guides/android/";
	String pic_9 = "http://top.sina.com.cn/news/show/industry/android/";
	String pic_10 = "http://www.ali213.net/news/game/";
	String pic_11 = "http://gl.ali213.net/new/";
	String pic_12 = "http://www.27270.com/tag/441.html";
	String pic_13 = "http://www.18183.com/mhxy/gonglue/";
	String pic_14 = "http://www.18183.com/yys/news/";
	String pic_15 = "http://www.18183.com/yys/gonglue/";
	String pic_16 = "http://www.18183.com/yxzjol/news/";
	String pic_17 = "http://www.18183.com/yxzjol/gonglue/";
	String pic_18 = "http://www.18183.com/lzgsy/news/";
	String pic_19 = "http://www.18183.com/lzgsy/gonglue/";
	String pic_20 = "http://www.ali213.net/news/zatan";
	
	
	@Override
	public void process(ResultItems  r, Task arg1) {
		/**
		 * 通过redis缓存取得数据存入数据库并放入数据缓存
		 * 这里可以通过验证数据库缓存查看是否获取过
		 */
		try {
			String herf =r.getRequest().getUrl();
			if(herf != null){
				if(!herf.equals(pic_) && !herf.equals(pic_2) && !herf.equals(pic_3) && !herf.equals(pic_4)
						&& !herf.equals(pic_5) && !herf.equals(pic_6) && !herf.equals(pic_7) && !herf.equals(pic_8)
						&& !herf.equals(pic_9) && !herf.equals(pic_10) && !herf.equals(pic_11) && !herf.equals(pic_12)
						&& !herf.equals(pic_13) && !herf.equals(pic_14) && !herf.equals(pic_15) && !herf.equals(pic_16)
						&& !herf.equals(pic_17) && !herf.equals(pic_18) && !herf.equals(pic_19) && !herf.equals(pic_20)){
					Tgrid t = (Tgrid) WZGLUtils.getCache(CacheConsts.CACHE_MAGIC_GIRD_NAME, herf);
					boolean b = service.saveMagicTgrids(t);
					if(b == false || t == null){
						System.out.println(herf+": 保存失败");
					}
					String url = jugeUrl(herf);
					String log = (String) WZGLUtils.getCache(CacheConsts.CACHE_LOG_MAGIC_NAME, url);
			   		log += "保存URL:"+herf+"数据.....";
			   		String log_cache = CacheConsts.CACHE_LOG_MAGIC_NAME;
	       		    String log_name = CacheConsts.CACHE_LOG_MAGIC_NAME+":"+url;
	       		    WZGLUtils.setCache(log_cache, log_name, log);
				}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	private String jugeUrl(String herf) {
		String url = "";
		if(herf.contains("pcgame")){
			url = pic_2;
		}else if(herf.contains("wen")){
			url = pic_;
		}else if(herf.contains("game/gl")){
			url = pic_3;
		}else if(herf.contains("163.com/shouyou")){
			url = pic_4;
		}else if(herf.contains("gg.163.com/")){
			url = pic_5;
		}else if(herf.contains("yxdown.com/news")){
			url = pic_6;
		}else if(herf.contains("news.17173")){
			url = pic_7;
		}else if(herf.contains("sina.com.cn/news/show/guides/android/")){
			url = pic_8;
		}else if(herf.contains("sina.com.cn/news/show/industry/android/")){
			url = pic_9;
		}else if(herf.contains("www.ali213.net/news/game/")){
			url = pic_10;
		}else if(herf.contains("gl.ali213.net/new/")){
			url = pic_11;
		}else if(herf.contains("www.27270.com/tag/441")){
			url = pic_12;
		}else if(herf.contains("/mhxy/gonglue/")){
			url = pic_13;
		}else if(herf.contains("/yys/news/")){
			url = pic_14;
		}else if(herf.contains("/yys/gonglue/")){
			url = pic_15;
		}else if(herf.contains("/yxzjol/news/")){
			url = pic_16;
		}else if(herf.contains("/yxzjol/gonglue/")){
			url = pic_17;
		}else if(herf.contains("/lzgsy/news/")){
			url = pic_18;
		}else if(herf.contains("/lzgsy/gonglue/")){
			url = pic_19;
		}else if(herf.contains("www.ali213.net/news/zatan")){
			url = pic_20;
		}
		
		return url;
	}

}
